package com.yh.csx.crm.appapi.controller;

import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.po.Customer;
import com.yh.csx.crm.po.CustomerVisitRecord;
import com.yh.csx.crm.service.CustomerService;
import com.yh.csx.crm.service.CustomerVisitService;
import com.yh.csx.crm.service.request.AppCustomerVisitReq;
import com.yh.csx.crm.service.request.AppCustomerVisitSearchReq;
import com.yh.csx.crm.service.response.AppCustomerVisitResp;
import com.yh.csx.crm.service.vo.PageWrap;
import com.yh.csx.crm.service.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Huang Zhaoping
 * @date 2019/3/1
 */
@Api("APP-客户拜访")
@RestController
@RequestMapping("/app/customer/visit")
public class AppCustomerVisitController extends BaseController {

    @Autowired
    private CustomerVisitService customerVisitService;

    @Autowired
    private CustomerService customerService;

    @ApiOperation("查询客户拜访记录")
    @GetMapping("/list")
    public CommonResponse<PageWrap<AppCustomerVisitResp>> listCustomerVisit(AppCustomerVisitSearchReq req) {
        return super.visit(() -> PageWrap.wrap(customerVisitService.findCustomerVisitList(req), a -> {
            AppCustomerVisitResp resp = BeanUtils.convert(AppCustomerVisitResp.class, a);
            if (a.getVisitImgsUrl() != null) resp.setVisitImgs(a.getVisitImgsUrl().split(","));
            return resp;
        }));
    }

    @ApiOperation("查询客户拜访记录详情")
    @GetMapping("/list/{id}")
    public CommonResponse<AppCustomerVisitResp> getVisitDetail(@PathVariable Long id) {
        return super.visit(() -> {
            CustomerVisitRecord record = customerVisitService.findCustomerVisitRecord(id);
            if (record == null) return null;
            AppCustomerVisitResp resp = BeanUtils.convert(AppCustomerVisitResp.class, record);
            Customer customer = customerService.getCustomer(resp.getCustomerId());
            if (customer != null) {
                resp.setCustomerNumber(customer.getCustomerNumber());
            }
            if (record.getVisitImgsUrl() != null) resp.setVisitImgs(record.getVisitImgsUrl().split(","));
            return resp;
        });
    }

    @ApiOperation("新增客户拜访详情")
    @PostMapping("/add")
    public CommonResponse<Void> addVisitRecord(@RequestBody AppCustomerVisitReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            customerVisitService.saveCustomerVisitRecord(user, req);
        });
    }
}
